from PyQt6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QFrame
from PyQt6.QtCore import Qt
from widgets.invite_page import InvitePage
from widgets.message_page import MessagePage
from widgets.batch_page import BatchPage
from widgets.friend_page import FriendPage
from utils.style_manager import StyleManager

class CenterPanel(QWidget):
    def __init__(self):
        super().__init__()
        self.current_function = "invite"
        self.setup_ui()
    
    def setup_ui(self):
        """设置UI"""
        layout = QVBoxLayout(self)
        layout.setSpacing(20)
        
        # 按钮区域
        buttons_frame = QFrame()
        buttons_layout = QHBoxLayout(buttons_frame)
        buttons_layout.setSpacing(15)
        
        # 群邀请按钮
        self.invite_btn = QPushButton("群邀请")
        self.invite_btn.setFixedSize(120, 40)
        self.invite_btn.clicked.connect(lambda: self.switch_function("invite"))
        StyleManager.set_button_style(self.invite_btn, "info")
        buttons_layout.addWidget(self.invite_btn)
        
        # 群发消息按钮
        self.message_btn = QPushButton("群发消息")
        self.message_btn.setFixedSize(120, 40)
        self.message_btn.clicked.connect(lambda: self.switch_function("message"))
        StyleManager.set_button_style(self.message_btn, "info")
        buttons_layout.addWidget(self.message_btn)
        
        # 批量拉群按钮
        self.batch_btn = QPushButton("批量拉群")
        self.batch_btn.setFixedSize(120, 40)
        self.batch_btn.clicked.connect(lambda: self.switch_function("batch"))
        StyleManager.set_button_style(self.batch_btn, "info")
        buttons_layout.addWidget(self.batch_btn)
        
        # 新好友按钮
        self.friend_btn = QPushButton("新好友/群聊")
        self.friend_btn.setFixedSize(120, 40)
        self.friend_btn.clicked.connect(lambda: self.switch_function("friend"))
        StyleManager.set_button_style(self.friend_btn, "info")
        buttons_layout.addWidget(self.friend_btn)
        
        layout.addWidget(buttons_frame)
        
        # 控制区域 - 使用堆叠布局来支持不同页面
        self.control_frame = QFrame()
        self.control_stack = QVBoxLayout(self.control_frame)
        
        # 创建四个不同的页面
        self.invite_page = InvitePage()
        self.message_page = MessagePage()
        self.batch_page = BatchPage()
        self.friend_page = FriendPage()
        
        # 默认显示群邀请页面
        self.show_control_page("invite")
        # 高亮当前功能按钮
        self.update_button_styles("invite")
        
        layout.addWidget(self.control_frame)
        
        # 主要数据表格 - 将在各个页面中创建
        self.main_table = None
        self.table_container = None
    
    def show_control_page(self, function_name):
        """显示指定的控制页面"""
        # 清除当前页面
        while self.control_stack.count():
            child = self.control_stack.takeAt(0)
            if child.widget():
                child.widget().setParent(None)
        
        # 显示新页面
        if function_name == "invite":
            self.control_stack.addWidget(self.invite_page)
        elif function_name == "message":
            self.control_stack.addWidget(self.message_page)
        elif function_name == "batch":
            self.control_stack.addWidget(self.batch_page)
        elif function_name == "friend":
            self.control_stack.addWidget(self.friend_page)
    
    def switch_function(self, function_name):
        """切换功能"""
        self.current_function = function_name
        self.show_control_page(function_name)
        self.update_button_styles(function_name)
    
    def update_button_styles(self, active_function):
        """更新按钮样式，突出显示当前选中的功能"""
        # 重置所有按钮样式为统一样式
        for btn in [self.invite_btn, self.message_btn, self.batch_btn, self.friend_btn]:
            StyleManager.set_button_style(btn, "info")
        
        # 突出显示当前选中的按钮
        active_button = None
        if active_function == "invite":
            active_button = self.invite_btn
        elif active_function == "message":
            active_button = self.message_btn
        elif active_function == "batch":
            active_button = self.batch_btn
        elif active_function == "friend":
            active_button = self.friend_btn
        
        if active_button:
            StyleManager.set_button_style(active_button, "success")